package com.bj.dz.controller;

import com.bj.dz.ResultModel.R;
import com.bj.dz.ResultModel.RHelper;
import com.bj.dz.ResultModel.ResponseCode;
import com.bj.dz.entity.ReleaseSubtask;
import com.bj.dz.entity.WaterBangData;
import com.bj.dz.service.WaterBangDataService;
import com.bj.dz.util.ExcelUtil;
import com.bj.dz.util.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/***
 *   控制器
 */
@Slf4j
@Api(tags = "【水泵】提供服务接口")
@RestController
@RequestMapping("/waterBangData")
public class WaterBangDataController {
    @Resource
    private WaterBangDataService waterBangDataService;

    /**
     * 查询所有发布任务【根据所关联子任务sid】
     *
     * @param
     * @return
     */
    @ApiOperation(value = "查询水泵数据，根据所关联sid")
    @RequestMapping(value = "/findAllBySid", method = RequestMethod.GET, produces = "application/json;" + "charset=UTF-8")
    public Page findAllByType(@ApiParam(name = "sid", value = "关联的sid", required = true)
                              @RequestParam(value = "sid", required = true) Integer sid,
                              @ApiParam(name = "page", value = "当前页", required = true) @RequestParam(value = "page", required = true) Integer page,
                              @ApiParam(name = "rows", value = "每页显示行数", required = true) @RequestParam(value = "rows", required = true) Integer rows) {
        log.info("关联的子任务id为:" + sid);
        //查询所有发布任务根据模块类型
        Map<String, Object> param = new HashMap<String, Object>();
        param.put("sid", sid);//子任务id
        Page<WaterBangData> pageData = waterBangDataService.queryWaterBangDataPageByMap(param, page, rows);//分页相关的一切数据
        return pageData;
    }

    /**
     * 通过id查询
     *
     * @param
     * @return
     */
    @ApiOperation(value = "查找服务【通过水泵数据的id查找】【使用】")
    @RequestMapping(value = "/findById", method = RequestMethod.POST, produces = "application/json;" + "charset=UTF-8")
    public R findWaterBangDataById(@ApiParam(name = "id", value = "水泵数据的id", required = true) @RequestParam(value = "id", required = true) Integer id) {
        R r = RHelper.ok();
        r.setDatas(waterBangDataService.getWaterBangDataById(id));
        return r;
    }

    /**
     * 查找多个服务【通过子任务id查询水泵数据】
     *
     * @param
     * @return
     */
    @ApiOperation(value = "查找多个服务【通过子任务id查询水泵数据】【使用】")
    @RequestMapping(value = "/findsBySid", method = RequestMethod.POST, produces = "application/json;" + "charset=UTF-8")
    public R findsBySid(@ApiParam(name = "sid", value = "子任务id", required = true) @RequestParam(value = "sid", required = true) Integer sid) {
        R r = RHelper.ok();
        Map<String, Object> param = new HashMap<String, Object>();
        param.put("sid", sid);
        List<WaterBangData> waterBangDataList = waterBangDataService.getWaterBangDataListByMap(param);
        r.setDatas(waterBangDataList);
        return r;
    }


    /**
     * 通过多个查询
     *
     * @param
     * @return
     */
    @ApiOperation(value = "查找多个服务")
    @RequestMapping(value = "/findWaterBangDatas", method = RequestMethod.POST, produces = "application/json;" + "charset=UTF-8")
    public List<WaterBangData> findWaterBangDataListByMap(@ApiParam(name = "WaterBangData", value = "waterBangData", required = true) @RequestBody WaterBangData waterBangData) {

        return waterBangDataService.getWaterBangDataListByMap(null);
    }

    /**
     * 查询总数
     *
     * @param
     * @return
     */
    @ApiOperation(value = "获取总数服务")
    @RequestMapping(value = "/count", method = RequestMethod.POST, produces = "application/json;" + "charset=UTF-8")
    public Integer WaterBangDataCount(@ApiParam(name = "sid", value = "对应子任务的sid", required = true) @RequestParam(value = "sid", required = true) Integer sid) {
        log.info("对应子任务的sid为:" + sid);
        //查询所有发布任务根据模块类型
        Map<String, Object> param = new HashMap<String, Object>();
        param.put("sid", sid);//模块类型
        return waterBangDataService.getWaterBangDataCountByMap(param);
    }

    /**
     * 添加水泵数据【传入子任务id和水泵的数据】
     *
     * @param
     * @return
     */
    @ApiOperation(value = "添加服务【使用】")
    @RequestMapping(value = "/add", method = RequestMethod.POST, produces = "application/json;" + "charset=UTF-8")
    public R addWaterBangData(@ApiParam(name = "WaterBangData", value = "水泵数据和对应子任务id【子任务id必填】", required = false) WaterBangData waterBangData) {
        R r = RHelper.ok();
        //获取子任务id
        Integer sid = waterBangData.getSid();
        if (sid == null || sid == 0) {
            r.setCode(ResponseCode.FAILED_CODE);
            r.setMsg("添加失败，未指定子任务");
            return r;
        }
        boolean flag = waterBangDataService.addWaterBangData(waterBangData);
        if (flag) {
            r.setMsg("添加水泵数据成功");
        } else {
            r.setCode(ResponseCode.FAILED_CODE);
            r.setMsg("添加水泵数据失败");
        }
        return r;
    }

    /**
     * 更新修改【传入子任务id和水泵的数据(还有id)】
     *
     * @param
     * @return
     */
    @ApiOperation(value = "更新修改服务【使用】")
    @RequestMapping(value = "/upd", method = RequestMethod.POST, produces = "application/json;" + "charset=UTF-8")
    public R modifyWaterBangData(@ApiParam(name = "WaterBangData", value = "水泵数据和对应子任务id【水泵id都必填】", required = false) WaterBangData waterBangData) {
        R r = RHelper.ok();
        //获取子任务id
        Integer sid = waterBangData.getSid();
        if (sid == null || sid == 0) {
            r.setCode(ResponseCode.FAILED_CODE);
            r.setMsg("更新失败，未指定子任务");
            return r;
        }
        //获取id
        Integer id = waterBangData.getId();
        if (id == null || id == 0) {
            r.setCode(ResponseCode.FAILED_CODE);
            r.setMsg("更新失败，未找到更新的水泵数据");
            return r;
        }
        boolean flag = waterBangDataService.modifyWaterBangData(waterBangData);
        if (flag) {
            r.setMsg("更新水泵数据成功");
        } else {
            r.setCode(ResponseCode.FAILED_CODE);
            r.setMsg("更新水泵数据失败");
        }
        return r;
    }

    /**
     * 子任务exce解析
     *
     * @param file
     * @param sid
     * @return
     */
    @ApiOperation(value = "子任务excel解析")
    @RequestMapping(value = "/upExcel", method = RequestMethod.POST, produces = "application/json;" + "charset=UTF-8")
    public boolean addReleaseSubtast(@ApiParam(name = "file", value = "上传的Excel文件", required = true) @RequestParam(value = "file", required = true) MultipartFile file,
                                     @ApiParam(name = "sid", value = "水泵对应的子任务sid", required = true) @RequestParam(value = "sid", required = true) Integer sid) {
        List<WaterBangData> waterBangData = null;
        boolean flag = false;
        try {
            waterBangData = ExcelUtil.importExcel(file.getInputStream(), "yyyy-MM-dd hh:mm:ss", 0, 1, 0, WaterBangData.class);
        } catch (IOException e) {
            e.printStackTrace();
        }
//        assert releaseSubtasks != null;
        for (WaterBangData w : waterBangData) {
            w.setSid(sid);
            flag = waterBangDataService.addWaterBangData(w);
            if (!flag) {
                return false;
            }
        }
        return flag;
    }

    /**
     * 删除
     *
     * @param
     * @return
     */
    @ApiOperation(value = "删除服务")
    @RequestMapping(value = "/del", method = RequestMethod.POST, produces = "application/json;" + "charset=UTF-8")
    public boolean deleteWaterBangDataById(@ApiParam(name = "WaterBangData", value = "waterBangData的id", required = true) @RequestParam(value = "id", required = true) Integer id) {
        return waterBangDataService.deleteWaterBangDataById(id);
    }

}
